# @time     :2022/2/28
# @Author   :Simple
# @FileName :conftest
import pytest
import requests

from util.handle_yaml import get_config_


def pytest_collection_modifyitems(items):
    """
    hook：
    测试用例收集完成时，将收集到的item的name和nodeid的中文显示在控制台上
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")


@pytest.fixture(scope="session")
def get_token() -> requests.Session():
    config_ = get_config_()
    url = config_['token']['url']
    corpid = config_['token']['corpid']
    corpsecret = config_['token']['corpsecret']
    params = {
        "corpid": corpid,
        "corpsecret": corpsecret
    }
    s = requests.Session()
    r = s.get(url, params=params)
    token = r.json()['access_token']
    # 共同参数放入session中，session会自动带入请求
    s.params = {'access_token': token}
    yield s
